//
//  RVMyCollectedViewController.m
//  RememberVocabularies
//
//  Created by 伍银荣 on 2017/11/2.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "RVMyCollectedViewController.h"
#import "RVTableViewController.h"


@interface RVMyCollectedViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) UISegmentedControl *segmentControl;
@property(nonatomic ,strong) UITableView *tableView;
@property(nonatomic, strong) NSArray *tableArr;

@end

@implementation RVMyCollectedViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setData];
    [self.tableView reloadData];
    self.tableView.tableFooterView = [UIView new];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的词库";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.segmentControl];
    self.segmentControl.selectedSegmentIndex = 0;
    
    [self.view addSubview:self.tableView];
    [self setData];
    [self.tableView reloadData];
}

-(void)setData
{
    NSString *bookPath = [[NSUserDefaults standardUserDefaults] valueForKey:@"nowPlan"][@"bookPath"];
    
    //拿到finishPlist文件的数据
    NSDictionary *finishDict = [NSDictionary dictionaryWithContentsOfFile:finishPlist];
    NSArray *arr1 = [finishDict valueForKey:bookPath];
    
    //拿到collectedPlist文件的数据
    NSDictionary *collectedDict = [NSDictionary dictionaryWithContentsOfFile:collectedPlist];
    NSArray *arr2 = [collectedDict valueForKey:bookPath];
    NSLog(@"%@:%@",arr1,arr2);
    
    self.tableArr = @[arr1,arr2];
}

-(UISegmentedControl *)segmentControl
{
    if (!_segmentControl) {
        UISegmentedControl *sc = [[UISegmentedControl alloc]initWithItems:@[@"已完成", @"已收藏"]];
        sc.bounds = CGRectMake(0, 70, 135, 30);
        sc.center = CGPointMake(self.view.frame.size.width / 2, 90);
        //默认no 瞬间弹起来，不维持选中状态
        //sc.momentary=YES;
        //默认选中第几个
        sc.selectedSegmentIndex = 0;
        //ios 5.0 以后才有的，根据内容的多少 去改变 宽度,no 不改变
        sc.apportionsSegmentWidthsByContent = YES;
        [sc addTarget:self action:@selector(segmentedControlClicked:) forControlEvents:UIControlEventValueChanged];
        _segmentControl = sc;
    }
    return _segmentControl;
}
-(NSArray *)tableArr
{
    if (!_tableArr) {
        _tableArr = [NSArray array];
    }
    return _tableArr;
}

-(void)segmentedControlClicked:(UISegmentedControl *)sender
{
    [self.tableView reloadData];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.segmentControl.frame) + 10, Width, Height - CGRectGetMaxY(self.segmentControl.frame) - 10) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        _tableView = tableView;
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger index = self.segmentControl.selectedSegmentIndex;
    if (index == 0) {
        return [self.tableArr[0] count];
    } else {
        return [self.tableArr[1] count];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *const cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if (self.segmentControl.selectedSegmentIndex == 0) {
        cell.textLabel.text = self.tableArr[0][indexPath.row];
    } else {
        cell.textLabel.text = self.tableArr[1][indexPath.row];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *word;
    if (self.segmentControl.selectedSegmentIndex == 0) {
        word = self.tableArr[0][indexPath.row];
    } else {
        word = self.tableArr[1][indexPath.row];
    }
    RVTableViewController *tableVC = [RVTableViewController new];
    tableVC.english = word;
    tableVC.navigationItem.title = @"单词释义";
    [self.navigationController pushViewController:tableVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48;
}


@end
